package com.szy.news.activity;
/**
 * @author 晋昱恢
 * 更多功能
 * 各大门户网站
 * 广告
 * */
import java.util.ArrayList;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MyViewPagerActivity extends Activity implements OnClickListener{
	//转接各大门户网站
	private LinearLayout QQ;
	private LinearLayout sinanews;
	private LinearLayout wynews;
	private LinearLayout sohunews;
	private LinearLayout NBA;
	private LinearLayout youku;
	private LinearLayout qidian;
	private LinearLayout kugou;
	private LinearLayout btn_back;
	//图片广告流的定义
    private int[] imgResIDs = new int[]
    { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e };
    private int[] radioButtonID = new int[]
    { R.id.radio0, R.id.radio1, R.id.radio2, R.id.radio3, R.id.radio4 };
    private ViewPager pager;
    private RadioGroup mGroup;
    private ArrayList<View> items = new ArrayList<View>();
    private int mCurrentItem = 0;
    private int mItem;
    private Runnable mPagerAction;
    private boolean isFrist = true;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        
        	//腾讯新闻
      		QQ = (LinearLayout) findViewById(R.id.QQ);
      		QQ.setOnClickListener(this);
      		//新浪新闻
      		sinanews = (LinearLayout) findViewById(R.id.sinanews);
      		sinanews.setOnClickListener(this);
      		//网易新闻
      		wynews = (LinearLayout) findViewById(R.id.wynews);
      		wynews.setOnClickListener(this);
      		//搜狐新闻
      		sohunews = (LinearLayout) findViewById(R.id.sohunews);
      		sohunews.setOnClickListener(this);
      		//NBA新闻
      		sohunews = (LinearLayout) findViewById(R.id.NBA);
      		sohunews.setOnClickListener(this);
      		//优酷视频
      		youku = (LinearLayout) findViewById(R.id.youku);
      		youku.setOnClickListener(this);
      		//起点中文网
      		qidian = (LinearLayout) findViewById(R.id.qidian);
      		qidian.setOnClickListener(this);
      		//酷狗音乐网
      		kugou = (LinearLayout) findViewById(R.id.kugou);
      		kugou.setOnClickListener(this);
      		//返回
      		btn_back = (LinearLayout) findViewById(R.id.more_back);
      		btn_back.setOnClickListener(this);
      		
        pager = (ViewPager) findViewById(R.id.tuijian_pager);
        mGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        initAllItems();
        pager.setAdapter(new PagerAdapter()
        {
            // 创建
            @Override
            public Object instantiateItem(View container, int position)
            {
                View layout = items.get(position % items.size());
                pager.addView(layout);
                return layout;
            }
            
            // 销毁
            @Override
            public void destroyItem(View container, int position, Object object)
            {
                View layout = items.get(position % items.size());
                pager.removeView(layout);
            }
            
            @Override
            public boolean isViewFromObject(View arg0, Object arg1)
            {
                return arg0 == arg1;
                
            }
            
            @Override
            public int getCount()
            {
                return imgResIDs.length;
            }
            
        });
        pager.setOnPageChangeListener(new OnPageChangeListener()
        {
            
            @Override
            public void onPageSelected(final int arg0)
            {
                mCurrentItem = arg0 % items.size();
                pager.setCurrentItem(mCurrentItem);
                mGroup.check(radioButtonID[mCurrentItem]);
//                items.get(arg0).findViewById(R.id.tuijian_header_img).setOnClickListener(new OnClickListener()
//                {
//                    @Override
//                    public void onClick(View v)
//                    {
//                        Toast.makeText(MyViewPagerActivity.this, arg0 + "", 1000).show();
//                    }
//                });
            }
            
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {
                // TODO Auto-generated method stub
            }
            
            @Override
            public void onPageScrollStateChanged(int arg0)
            {
                // TODO Auto-generated method stub
                
            }
        });
        mPagerAction = new Runnable()
        {
            @Override
            public void run()
            {
                
                if (mItem != 0)
                {
                    if (isFrist == true)
                    {
                        mCurrentItem = 0;
                        isFrist = false;
                    }
                    else
                    {
                        if (mCurrentItem == items.size() - 1)
                        {
                            mCurrentItem = 0;
                        }
                        else
                        {
                            mCurrentItem++;
                        }
                    }
                    
                    pager.setCurrentItem(mCurrentItem);
                    mGroup.check(radioButtonID[mCurrentItem]);
                    
                }
                pager.postDelayed(mPagerAction, 2500);
            }
        };
        pager.postDelayed(mPagerAction, 100);
    }
    
    private void initAllItems()
    {
        // 初始化Viewpager的所有item
        for (int i = 0; i < imgResIDs.length; i++)
        {
            items.add(initPagerItem(imgResIDs[i]));
        }
        mItem = items.size();
    }
    
    private View initPagerItem(int resID)
    {
        View layout1 = getLayoutInflater().inflate(R.layout.tuijian_header, null);
        ImageView imageView1 = (ImageView) layout1.findViewById(R.id.tuijian_header_img);
        imageView1.setImageResource(resID);
        return layout1;
    }

	@Override
	public void onClick(View v) {
		int id = v.getId();
		final Intent intent;
		if(id == R.id.QQ){
			intent = new Intent();
			intent.setData(Uri.parse("http://news.qq.com/"));
			intent.setAction(Intent.ACTION_VIEW);
			startActivity(intent);
		}else if(id == R.id.sinanews){
			intent = new Intent();
			intent.setData(Uri.parse("http://news.sina.com/"));
			intent.setAction(Intent.ACTION_VIEW);
			startActivity(intent);
		}else if(id == R.id.wynews){
			intent = new Intent();
			intent.setData(Uri.parse("http://news.163.com/"));
			intent.setAction(Intent.ACTION_VIEW);
			startActivity(intent);
		}else if(id == R.id.sohunews){
			intent = new Intent();
			intent.setData(Uri.parse("http://news.sohu.com/"));
			intent.setAction(Intent.ACTION_VIEW);
			startActivity(intent);
		}else if(id == R.id.NBA){
			intent = new Intent();
			intent.setData(Uri.parse("http://hao.360.cn/nba.html"));
			intent.setAction(Intent.ACTION_VIEW);
			startActivity(intent);
		}else if(id == R.id.youku){
			intent = new Intent();
			intent.setData(Uri.parse("http://www.youku.com/"));
			intent.setAction(Intent.ACTION_VIEW);
			startActivity(intent);
		}else if(id == R.id.qidian){
			intent = new Intent();
			intent.setData(Uri.parse("http://www.qidian.com/"));
			intent.setAction(Intent.ACTION_VIEW);
			startActivity(intent);
		}else if(id == R.id.kugou){
			intent = new Intent();
			intent.setData(Uri.parse("http://www.kugou.com/"));
			intent.setAction(Intent.ACTION_VIEW);
			startActivity(intent);
		}else if(id == R.id.more_back){
			intent = new Intent();
			intent.setClass(MyViewPagerActivity.this, SetActivity.class);
			startActivity(intent);
			finish();
		}
	}
}
